package com.kzd.btgame.main.game

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setEmptyView
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.custom.setTransitionStatusT
import com.kzd.btgame.databinding.IncludeAppRefreshBinding
import com.kzd.btgame.entity.GameBean
import com.kzd.btgame.entity.TravelTab
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.checkLogin
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.kzd.btgame.main.home.adapter.TravelReserveAdapter
import com.kzd.btgame.main.home.adapter.TravelTodayAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class GameTravelFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>() {

    companion object {
        const val KEY_TRAVEL_TYPE = "KEY_TRAVEL_TYPE"
        fun newInstance(tab: TravelTab): GameTravelFragment {
            val bundle: Bundle = Bundle().apply {
                putSerializable(KEY_TRAVEL_TYPE, tab)
            }
            return GameTravelFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mTravelTab: TravelTab? by lazy {
        arguments?.getSerializable(KEY_TRAVEL_TYPE) as? TravelTab
    }

    private val mTravelTodayAdapter = TravelTodayAdapter()

    private val mGame1Adapter = Game1Adapter<GameBean>(
        Game1Adapter.Type.GAME_LIST_RANKING_1
    )

    private val mTravelReserveAdapter = TravelReserveAdapter()

    private val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {
        mTravelTab?.let {
            val adapter = when (it.type) {
                TravelTab.Type.TODAY -> mTravelTodayAdapter
                TravelTab.Type.WEEK -> mGame1Adapter
                TravelTab.Type.RESERVE -> mTravelReserveAdapter
            }
            mViewBinding.rvAppRefreshBody.apply {
                layoutManager = LinearLayoutManager(context)
                itemAnimator = null
            }.adapter = adapter
        }

        mTravelReserveAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mTravelReserveAdapter.getItem(position)
            val gid = itemData.id
                ?: return@setOnItemChildClickListener
            val type = if (itemData.is_yuyue)
                "1" else "0"
            if (view.id == R.id.tvTravelReserveSubmit) checkLogin {
                mViewModel.reserveChange(gid, type, position)
            }
        }


        mTravelTab?.let {
            when (it.type) {
                TravelTab.Type.TODAY -> {
                    mViewBinding.mSmartRefreshLayout.setEnableLoadMore(true)
                }
                TravelTab.Type.WEEK -> {
                    mViewBinding.mSmartRefreshLayout.setEnableLoadMore(false)
                }
                TravelTab.Type.RESERVE -> {
                    mViewBinding.mSmartRefreshLayout.setEnableLoadMore(true)
                }
            }
        }

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            initData()
        }
        mViewBinding.mSmartRefreshLayout.setOnLoadMoreListener {
            initData(isLoadMore = true)
        }
    }

    override fun initData() {
        initData(isLoadMore = false)
    }

    private fun initData(isLoadMore: Boolean = false) {
        mTravelTab?.let {
            when (it.type) {
                TravelTab.Type.TODAY -> {
                    mViewModel.travelToday(isLoadMore)
                }
                TravelTab.Type.WEEK -> {
                    mViewModel.travelWeek()
                }
                TravelTab.Type.RESERVE -> {
                    mViewModel.travelReserve(isLoadMore)
                }
            }
        }
    }

    override fun initViewMode() {
        mViewModel.travelTodayLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusT(response, mTravelTodayAdapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.list
            if (data.isNullOrEmpty()) {
                mTravelTodayAdapter.setEmptyView()
                return@observe
            }
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mTravelTodayAdapter.addData(data)
            } else {
                mTravelTodayAdapter.setNewInstance(data)
            }
        })

        mViewModel.travelWeekLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mGame1Adapter, mViewBinding.mSmartRefreshLayout, (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mGame1Adapter.setNewInstance(data)
        })

        mViewModel.travelReserveLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusT(response, mTravelReserveAdapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.list
            if (data.isNullOrEmpty()) {
                mTravelReserveAdapter.setEmptyView()
                return@observe
            }
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mTravelReserveAdapter.addData(data)
            } else {
                mTravelReserveAdapter.setNewInstance(data)
            }
        })

        mViewModel.reserveChangeLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val position = response.data?.position
                ?: return@observe
            val data = response.data?.data
                ?: return@observe
            mTravelReserveAdapter.setData(position, data)
        })
    }

}